package com.mnxiaotu.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.FileNotFoundException;


/**
 * Web 项目配置
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                // 设置允许的header属性
                .allowedHeaders("*")
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/userAvatar/**")
                //D:\VueProjects\rbac-system\rbac-permission
                .addResourceLocations("file:D:\\VueProjects\\rbac-system\\rbac-permission\\src\\main\\resources\\userAvatar\\")
                // 项目启动之后要确认被加载
                .addResourceLocations("classpath:/userAvatar/");
//        registry.addResourceHandler("/image/userAvatar/**")
//                .addResourceLocations("file:D:\\Study\\Codes\\xc_permission_mgt\\xc_back\\src\\main\\resources\\image\\userAvatar\\")
//                .addResourceLocations("classpath:/image/userAvatar/");// 项目启动之后要确认被加载
        // 获取项目根目录的存放路径
//        String rootPath = System.getProperty("user.dir");
//        String avatarPath = rootPath + "/src/main/resources/userAvatar/";
//
//        registry.addResourceHandler("/userAvatar/**")
//                .addResourceLocations("file:" + avatarPath)
//                .addResourceLocations("classpath:/userAvatar/");
    }
}
